//
//  Kakin_settingViewController.m
//  01_kakin
//
//  Created by jon due on 12/03/21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Kakin_settingViewController.h"

#import "setting_Date.h"

#define tag_alerm   11
#define tag_screen  12


@implementation Kakin_settingViewController

@synthesize kit = _kit;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.settingTable.backgroundColor = [ UIColor clearColor ];
    self.bgImgView.image = [ UIImage imageNamed:IMG_settingBG ];
    
    self.settingAry = [[ NSArray alloc ]initWithObjects:@"名前", @"アラーム ON/OFF", @"アラーム時間", @"自動ロック",@"アドオン", nil];
}

-(void)dealloc{
    [ _kit release ];
    [ super dealloc ];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL)animated{
    [ super viewWillAppear:animated ];
    
    [ self.settingTable reloadData ];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

/* ============================================================================== */
#pragma mark - override method
/* ============================================================================== */

-(NSInteger)sectionSize{
    return 1;
}
-(NSInteger)rowSize:(NSInteger)section{
        
    return [ self.settingAry count ];
}

// cell作成
-(UITableViewCell*)createCell:(NSIndexPath *)path{
    
    if(path.row == 0 || path.row == 2 || path.row == 4){
    
        settingNormalCell *cell = nil;
        static NSString *kAlertCell_ID = @"settingNormalCell";
        cell = [self.settingTable dequeueReusableCellWithIdentifier:kAlertCell_ID];
        
        if (cell == nil){
            cell = [[[ NSBundle mainBundle ] loadNibNamed:@"settingNormalCell" 
                                                    owner:self
                                                  options:nil]lastObject];
        }
        
        cell.titleLabel.text = [ self.settingAry objectAtIndex:path.row ];
        
        // 
        if(path.row == 0){
            // 名前
            cell.subLabel.text = [ DefaultUril readDefaultsString:key_userName ];
            
        }else if(path.row == 2){
            // アラーム時間
            cell.subLabel.text = [ SADateUtil TimeToFormatString:[ DefaultUril readDefaultsDate:key_AlermTime ] ];
            
        }else{
            cell.subLabel.text = @"アドオン";
        }
           
        return cell;
        
    }else{
        
        settingSwitchCell *cell = nil;
        static NSString *kAlertCell_ID = @"settingSwitchCell";
        cell = [self.settingTable dequeueReusableCellWithIdentifier:kAlertCell_ID];
        
        if (cell == nil){
            cell = [[[ NSBundle mainBundle ] loadNibNamed:@"settingSwitchCell" 
                                                    owner:self
                                                  options:nil]lastObject];
            // 選択させない
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.titleLabel.text = [ self.settingAry objectAtIndex:path.row ];
        cell.selected = NO;
        
        // スイッチin/off
        if (path.row == 1) {
            // アラーム
            [cell.subSeitch setOn:[ DefaultUril readDefaultsInteger:key_isAlert ] ];
            cell.subSeitch.tag = tag_alerm;
            
        }else if(path.row == 3){
            // ロック
            [cell.subSeitch setOn:[ DefaultUril readDefaultsInteger:key_isScreen ] ];
            cell.subSeitch.tag = tag_screen;
        }
        [ cell.subSeitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged ];
        
        return cell;
    }
    
    return nil;
}

/* delegate */
-(void)selectTableRow:(NSIndexPath*)path{
    
    // 
    if(path.row == 0){
        // 名前入力画面へ
        
    }else if(path.row == 2 ){
        // dataPicker表示画面へ
        setting_Date *setDataView = [[ setting_Date alloc ]initWithNibName:@"setting_Date" bundle:nil];
        [ (SANavigationController*)self.navigationController SApushViewController:setDataView animated:SAAnimationTypeRight ];
        [ setDataView release ];
        
    }else if(path.row == 4){
        
        self.kit = [[ SASroreKit alloc ]initStoreKit];
        self.kit.delegate = self;
        [ self.kit startCheck ];
    }
    
}

/* ============================================================================== */
#pragma mark - switch Action
/* ============================================================================== */

-(void)changeSwitch:(UISwitch*)sw{
    
    if(sw.tag == tag_alerm){
        
        [ DefaultUril writeDefaultsInteger:key_isAlert :sw.on ];
        
        // APPDelegateを取得
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        // アラームの予約
        if(sw.on){
            // アラームセット (毎日同じ時間にアラームが流れる)
            NSDate *date = [ DefaultUril readDefaultsDate:key_AlermTime ];
            NSArray *ary = [ SADateUtil hourMinToFormatString:date ];
            [app addAlerm:[ [ ary objectAtIndex:0] intValue ] :[ [ ary objectAtIndex:1]intValue ] file:SOUND_Alerm];
            
        }else{
            // アラーム解除
            [ app clearAlerm ];
        }
        
    }else if(sw.tag == tag_screen){
        
        [ DefaultUril writeDefaultsInteger:key_isScreen :sw.on ];
    }
    
}

/* ============================================================================== */
#pragma mark - SASroreKitDelegate
/* ============================================================================== */

-(void)didFaildNetwork:(NSString*)errstr{
    NSLog(@"didFaildNetwork");
}
-(void)didFaildDevice:(NSString*)errstr{
    NSLog(@"didFaildDevice");
}


@end
